package com.lancoo.answer.ui.child.original;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lancoo.answer.R;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.ExamViewBean;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.util.paperUtils.FragmentCommunicationUtils;
import com.lancoo.answer.widget.audioPlayView.AudioPlayCallBack;
import com.lancoo.answer.widget.combinationView.AnswerAnalysisView;
import com.lancoo.answer.widget.combinationView.FillAnswerResultView;
import com.lancoo.answer.widget.combinationView.OptionOriginalView;
import com.lancoo.answer.widget.combinationView.QueseBodyView;

import java.util.List;


/**
 * Created by pengmi on 2021/12/1 20:53
 * Description:单选，阅读理解，阅读判断小题查看评阅Fragment
 */
public class ChildSelectOriginalFragment extends Fragment {

    private QueseBodyView tv_queseBody;
    private AnswerAnalysisView answerAnalysisView;
    private OptionOriginalView optionComponent;
    private FillAnswerResultView fillAnswerResultView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.ev_child_select_original, container, false);
    }

    public static Fragment loadChildFragment(int kindIndex, int queseIndex, int smallQueseIndex){
        ChildSelectOriginalFragment queseFragment=new ChildSelectOriginalFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX,kindIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX,queseIndex);
        mBundle.putInt(AnswerConstant.KEY_CHILDINDEX,smallQueseIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view){
        if(getArguments()==null){
            return;
        }
        int typeIndex=getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        int queseIndex=getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        int childIndex=getArguments().getInt(AnswerConstant.KEY_CHILDINDEX);
        ExamViewBean constantExamBean = ConstantBean.Companion.getConstantExamBean();
        if(constantExamBean==null){
            return;
        }
        List<Type> typeList = constantExamBean.getTypeList();
        if(typeList==null||typeIndex>=typeList.size()){
            return;
        }
        List<Ques> quesList = typeList.get(typeIndex).getQuesList();
        if(quesList==null||queseIndex>=quesList.size()){
            return;
        }
        Ques ques = quesList.get(queseIndex);
        List<Child> childList = ques.getChildList();
        if(childList==null||childIndex>=childList.size()){
            return;
        }
        Child child = childList.get(childIndex);
        tv_queseBody = view.findViewById(R.id.quese_body_view);
        tv_queseBody.initData(child);
        answerAnalysisView=view.findViewById(R.id.answer_analysis_view);
        optionComponent=view.findViewById(R.id.option_component);
        tv_queseBody.setCallBack(new AudioPlayCallBack() {
            @Override
            public void onAudioPlay() {
                super.onAudioPlay();
                FragmentCommunicationUtils.closeAllMediaPlay(getParentFragment());
            }
        });
        optionComponent.setChild(child);
        fillAnswerResultView=view.findViewById(R.id.fill_answer_result);
        if(TextUtils.equals("14",ques.getGenreID())){
            answerAnalysisView.setVisibility(View.GONE);
            fillAnswerResultView.setVisibility(View.VISIBLE);
            fillAnswerResultView.setHideScore(true);
            fillAnswerResultView.setFillResult(child,ques.getGenreID());
        }else{
            answerAnalysisView.setVisibility(View.VISIBLE);
            fillAnswerResultView.setVisibility(View.GONE);
            answerAnalysisView.setAnalysis(child.getChildAnalysis());
        }
        if(TextUtils.equals("k",ques.getTypeNo())&&child.getChildAnswerType()!=0){
            tv_queseBody.setVisibility(View.GONE);
        }
    }


    @Override
    public void onPause() {
        super.onPause();
        pauseChildAudioPlay();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(tv_queseBody!=null){
            tv_queseBody.release();
        }
    }

    /**
     * 暂停小题音频播放
     */
    public void pauseChildAudioPlay(){
        if(tv_queseBody!=null){
            tv_queseBody.pause();
        }
    }


    public void adjustFontSize(){
        if(tv_queseBody!=null){
            tv_queseBody.adjustFontSize();
            fillAnswerResultView.adjustFontSize();
            answerAnalysisView.adjustFontSize();
            optionComponent.adjustFontSize();
        }
    }
}
